﻿using System;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public string director = "";

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            folderBrowserDialog1.SelectedPath = "d:\\";
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                this.textBox1.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            string dir = this.textBox1.Text;
            deletefile(dir);

            MessageBox.Show("ok");
        }

        private void deletefile(string dir)
        {
            DirectoryInfo d1 = new DirectoryInfo(dir);
            FileInfo[] f1 = d1.GetFiles();

            foreach (FileInfo f2 in f1)
            {
                if (f2.Extension == ".cs")
                {
                    f2.Delete();
                }
            }

            DirectoryInfo[] d2 = d1.GetDirectories();

            foreach (DirectoryInfo d3 in d2)
            {
                deletefile(d3.FullName);
            }
        }

        private void changefile(string dir)
        {
            string houzhui = this.textBox_houzhui.Text;
            DirectoryInfo d1 = new DirectoryInfo(dir);
            FileInfo[] f1 = d1.GetFiles();

            foreach (FileInfo f2 in f1)
            {
                if (Path.GetExtension(f2.Name) != "." + houzhui)
                {
                    f2.CopyTo(Path.Combine(f2.DirectoryName, Path.GetFileNameWithoutExtension(f2.Name) + "." + houzhui));
                    f2.Delete();
                }
            }

            DirectoryInfo[] d2 = d1.GetDirectories();

            foreach (DirectoryInfo d3 in d2)
            {
                changefile(d3.FullName);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.textBox1.Text = director;
        }

        private void button_xiugaihouzhui_Click(object sender, EventArgs e)
        {
            if (this.textBox_houzhui.Text == "")
            {
                MessageBox.Show("填写后缀...");
                return;
            }

            string dir = this.textBox1.Text;
            changefile(dir);

            MessageBox.Show("ok");
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            string dir = this.textBox1.Text;
            //01.03 酒吧经验.txt 变成 2009-01-03
            //主要是字符串操作
            DirectoryInfo di = new DirectoryInfo(dir);
            FileInfo[] fi = di.GetFiles("*.txt", SearchOption.AllDirectories);
            foreach (var item in fi)
            {
                string dirinfo = Path.GetDirectoryName(item.FullName);
                string name = Path.GetFileNameWithoutExtension(item.FullName);

                string oldname = name.Substring(0, 5);
                if (oldname.IndexOf("-") != -1) { continue; }
                string content = name.Substring(6, name.Length - 6);

                string[] sl = oldname.Split('.');
                string newname = "2007-" + sl[0] + "-" + sl[1] + " " + content + ".txt";

                item.CopyTo(Path.Combine(dirinfo, newname));
                item.Delete();
            }
        }

        private void button2_Click_1(object sender, EventArgs e)
        {

        }
    }
}